Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make gate counting functions less confusing and avoid estimations #9046

Merged
merged 17 commits into from
Oct 9, 2024

Conversation

lucasxia01
Copy link
Contributor

@lucasxia01 lucasxia01 commented Oct 7, 2024

Removes unnecessary gate counting in Honk flows in main.cpp and instead inits the SRS based on the actual finalized gate count taken from the Prover.

Renames get_num_gates to get_estimated_num_finalized_gates, renames a few other functions to add "estimated" to their names to reflect their actual functionality and avoid misuse.

Replace estimating functions used in main.cpp and in c_bind.cpp with functions that return the actual finalized gate count.

Fixes a bug in an earlier PR #9042, which forgot the ensure_nonzero = true argument to finalize_circuit(), and undercounted the number of gates in the circuit, leading a smaller than needed SRS.

@lucasxia01 lucasxia01 self-assigned this Oct 7, 2024
@lucasxia01 lucasxia01 marked this pull request as ready for review October 7, 2024 16:12
Copy link
Contributor

github-actions bot commented Oct 7, 2024

Changes to circuit sizes

Generated at commit: 04d8f4edc3fcf59663bb8dd76ddc4cdddc54e89a, compared to commit: 72e4867fc0c429563f7c54092470010d1e6553a9

🧾 Summary (100% most significant diffs)

Program ACIR opcodes (+/-) % Circuit size (+/-) %
public_kernel_inner_simulated 0 ➖ 0.00% +15 ❌ +1500.00%
public_kernel_merge_simulated 0 ➖ 0.00% +15 ❌ +1500.00%
public_kernel_tail_simulated 0 ➖ 0.00% +15 ❌ +1500.00%
rollup_base_simulated 0 ➖ 0.00% +15 ❌ +1500.00%
empty_nested 0 ➖ +∞% +17 ❌ +425.00%
empty_nested_simulated 0 ➖ 0.00% +17 ❌ +425.00%
private_kernel_empty_simulated 0 ➖ 0.00% +17 ❌ +425.00%
private_kernel_init_simulated 0 ➖ 0.00% +17 ❌ +425.00%
private_kernel_inner_simulated 0 ➖ 0.00% +17 ❌ +425.00%
private_kernel_reset_simulated 0 ➖ 0.00% +17 ❌ +425.00%
private_kernel_reset_simulated_4_4_4_4_4_4_4_4_1 0 ➖ 0.00% +17 ❌ +425.00%
private_kernel_tail_simulated 0 ➖ 0.00% +17 ❌ +425.00%
private_kernel_tail_to_public_simulated 0 ➖ 0.00% +17 ❌ +425.00%
rollup_block_root_empty 0 ➖ 0.00% +15 ❌ +0.52%
private_kernel_empty 0 ➖ 0.00% +17 ❌ +0.49%
private_kernel_tail 0 ➖ 0.00% +17 ❌ +0.19%
private_kernel_init 0 ➖ 0.00% +17 ❌ +0.05%
parity_base 0 ➖ 0.00% +15 ❌ +0.05%
private_kernel_tail_to_public 0 ➖ 0.00% +17 ❌ +0.04%
private_kernel_inner 0 ➖ 0.00% +17 ❌ +0.03%
private_kernel_reset_4_4_4_4_4_4_4_4_1 0 ➖ 0.00% +17 ❌ +0.02%
private_kernel_reset 0 ➖ 0.00% +17 ❌ +0.00%
public_kernel_inner 0 ➖ 0.00% +14 ❌ +0.00%
public_kernel_merge 0 ➖ 0.00% +14 ❌ +0.00%
rollup_merge 0 ➖ 0.00% +14 ❌ +0.00%
rollup_root 0 ➖ 0.00% +14 ❌ +0.00%
rollup_block_merge 0 ➖ 0.00% +14 ❌ +0.00%
public_kernel_tail 0 ➖ 0.00% +14 ❌ +0.00%
rollup_block_root 0 ➖ 0.00% +14 ❌ +0.00%
rollup_base 0 ➖ 0.00% +14 ❌ +0.00%
parity_root 0 ➖ 0.00% +14 ❌ +0.00%

Full diff report 👇
Program ACIR opcodes (+/-) % Circuit size (+/-) %
public_kernel_inner_simulated 1 (0) 0.00% 16 (+15) +1500.00%
public_kernel_merge_simulated 1 (0) 0.00% 16 (+15) +1500.00%
public_kernel_tail_simulated 1 (0) 0.00% 16 (+15) +1500.00%
rollup_base_simulated 1 (0) 0.00% 16 (+15) +1500.00%
empty_nested 0 (0) +∞% 21 (+17) +425.00%
empty_nested_simulated 1 (0) 0.00% 21 (+17) +425.00%
private_kernel_empty_simulated 1 (0) 0.00% 21 (+17) +425.00%
private_kernel_init_simulated 1 (0) 0.00% 21 (+17) +425.00%
private_kernel_inner_simulated 1 (0) 0.00% 21 (+17) +425.00%
private_kernel_reset_simulated 1 (0) 0.00% 21 (+17) +425.00%
private_kernel_reset_simulated_4_4_4_4_4_4_4_4_1 1 (0) 0.00% 21 (+17) +425.00%
private_kernel_tail_simulated 1 (0) 0.00% 21 (+17) +425.00%
private_kernel_tail_to_public_simulated 1 (0) 0.00% 21 (+17) +425.00%
rollup_block_root_empty 126 (0) 0.00% 2,923 (+15) +0.52%
private_kernel_empty 671 (0) 0.00% 3,486 (+17) +0.49%
private_kernel_tail 4,744 (0) 0.00% 9,041 (+17) +0.19%
private_kernel_init 25,003 (0) 0.00% 32,283 (+17) +0.05%
parity_base 5,371 (0) 0.00% 32,293 (+15) +0.05%
private_kernel_tail_to_public 29,828 (0) 0.00% 39,510 (+17) +0.04%
private_kernel_inner 44,042 (0) 0.00% 52,354 (+17) +0.03%
private_kernel_reset_4_4_4_4_4_4_4_4_1 34,897 (0) 0.00% 74,479 (+17) +0.02%
private_kernel_reset 91,933 (0) 0.00% 467,730 (+17) +0.00%
public_kernel_inner 268,759 (0) 0.00% 516,578 (+14) +0.00%
public_kernel_merge 53,490 (0) 0.00% 1,103,560 (+14) +0.00%
rollup_merge 3,673 (0) 0.00% 1,896,246 (+14) +0.00%
rollup_root 41,521 (0) 0.00% 1,983,367 (+14) +0.00%
rollup_block_merge 41,537 (0) 0.00% 1,983,383 (+14) +0.00%
public_kernel_tail 258,424 (0) 0.00% 2,270,260 (+14) +0.00%
rollup_block_root 4,188 (0) 0.00% 2,837,331 (+14) +0.00%
rollup_base 671,888 (0) 0.00% 3,525,994 (+14) +0.00%
parity_root 5,399 (0) 0.00% 3,775,125 (+14) +0.00%

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change the .hbs files that are used to generate these? Otherwise they will be rolled back next time we generate our code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be done

@lucasxia01 lucasxia01 changed the title fix: ensure_nonzero usage for finalize_circuit fix: make gate counting functions less confusing and avoid estimations Oct 7, 2024
@@ -142,23 +142,15 @@ UltraWithKeccakVerifier UltraComposer::create_ultra_with_keccak_verifier(Circuit
return output_state;
}

size_t UltraComposer::compute_dyadic_circuit_size(CircuitBuilder& circuit)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant, could just call the builder functions that do the exact same thing

// Construct Honk proof
Prover prover{ builder };
init_bn254_crs(prover.proving_key->proving_key.circuit_size);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core change: avoiding a needless builder computation just to get an incorrect srs size...

@@ -701,7 +697,8 @@ template <typename Builder = UltraCircuitBuilder> void gateCount(const std::stri
for (auto constraint_system : constraint_systems) {
auto builder = acir_format::create_circuit<Builder>(
constraint_system, 0, {}, honk_recursion, std::make_shared<bb::ECCOpQueue>(), true);
builder.finalize_circuit();
builder.finalize_circuit(/*ensure_nonzero=*/true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core change: fix thats needed to properly add in the ensure nonzero gates

void AcirComposer::create_circuit(acir_format::AcirFormat& constraint_system,
WitnessVector const& witness,
bool collect_gates_per_opcode)
void AcirComposer::create_finalized_circuit(acir_format::AcirFormat& constraint_system,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

core change: finalizing the circuit here

@@ -32,13 +32,23 @@ class AcirComposer {
bool verify_proof(std::vector<uint8_t> const& proof);

std::string get_solidity_verifier();
size_t get_total_circuit_size() { return builder_.get_total_circuit_size(); };
size_t get_dyadic_circuit_size() { return builder_.get_circuit_subgroup_size(builder_.get_total_circuit_size()); };
size_t get_finalized_total_circuit_size() { return builder_.get_finalized_total_circuit_size(); };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two new functions that give a finalized size (and check that the circuit is actually finalized)

auto total_with_extra_gates = builder.get_total_circuit_size() + num_extra_gates;
*total = htonl((uint32_t)total_with_extra_gates);
*subgroup = htonl((uint32_t)builder.get_circuit_subgroup_size(builder.get_total_circuit_size()));
builder.finalize_circuit(/*ensure_nonzero=*/true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we just overestimate if we ever have to pick. This function is used by plonk and honk, so we just go with adding more gates

Copy link
Contributor

@ledwards2225 ledwards2225 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just a few minor notes

@lucasxia01 lucasxia01 enabled auto-merge (squash) October 8, 2024 22:52
@lucasxia01 lucasxia01 merged commit 0bda0a4 into master Oct 9, 2024
51 checks passed
@lucasxia01 lucasxia01 deleted the lx/fix-finalize-circuit-calls branch October 9, 2024 00:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants